package cn.sxt.yxl.user.service.impl;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.user.dao.IAdminDao;
import cn.sxt.yxl.user.dao.IRoleDao;
import cn.sxt.yxl.user.pojo.Admin;
import cn.sxt.yxl.user.pojo.Role;
import cn.sxt.yxl.user.service.IUserService;

@Service("userService")
//@Transactional
public class UserServiceImpl implements IUserService{
	
	@Resource
	private IAdminDao adminDao;
	@Resource
	private IRoleDao roleDao;
	
	@Override
	public Role findOneRoleSercice(Map<String, Object> map) {
			
		return roleDao.findOne(map);
	}
	
	@Override
	public JSONObject saveOneRoleService(Role role) {
		JSONObject resultJSON = new JSONObject();
		int res = roleDao.saveOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneRoleService(Role role) {
		JSONObject resultJSON = new JSONObject();
		int res = roleDao.updateOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}

	@Override
	public JSONObject deleteOneRoleService(Role role) {
		JSONObject resultJSON = new JSONObject();
		int res = roleDao.deleteOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}
	
	@Override
	public Admin findOneAdminSercice(Map<String, Object> map) {
			
		return adminDao.findOne(map);
	}
	
	@Override
	public JSONObject saveOneAdminService(Admin admin) {
		JSONObject resultJSON = new JSONObject();
		int res = adminDao.saveOne(admin);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	@Override
	public JSONObject updateOneAdminService(Admin admin) {
		JSONObject resultJSON = new JSONObject();
		int res = adminDao.updateOne(admin);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}

	@Override
	public JSONObject deleteOneAdminService(Admin admin) {
		JSONObject resultJSON = new JSONObject();
		int res = adminDao.deleteOne(admin);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admin.getId()+"");
			dataJSON.put("effectCount", res);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
		} else {
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;

	}

	@Override
	public List<Role> findRoleListService(PageInfoUtil pageInfoUtil ,Map<String, Object> map) {
		if(pageInfoUtil != null){
			Page<Role> page = PageHelper.startPage(
					pageInfoUtil.getCurrentPage() , pageInfoUtil.getPageSize());
			List<Role> roleList = roleDao.findList(map);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() +""));
			return roleList;
		}
		return roleDao.findList(map);
	}

	@Override
	public List<Admin> findAdminListService(PageInfoUtil pageInfoUtil,Map<String, Object> map) {
		if(pageInfoUtil != null){
			Page<Admin> page = PageHelper.startPage(
					pageInfoUtil.getCurrentPage() , pageInfoUtil.getPageSize());
			List<Admin> adminList = adminDao.findList(map);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() +""));
			return adminList;
		}
		return adminDao.findList(map);
	}

}
